const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",//id
    manager_name:"",//项目负责人
    manager_num:"",//项目负责人上传号
    gybz:"",//工艺编制
    bzrq:"",//编制日期
    show_bzrq:false,//编制日期
    gybb:"",//工艺版本
    yxj:"",//优先级
    start_time:"",//生产准备开始时间
    show_start_time:false,
    end_time:"",//生产结束时间
    show_end_time:false,
    product_type:"",//产品类型
    material_no:"",//材质
    material_method:"",//下料方式
    mpsl:"",//毛坯数量
    num:"",//加工数量
    mindate:Math.round((new Date() - 365*86400*1000)),
    __returnParams: null,
    setManager:null,
    material_attr:"",
    material_id:"",//材质id
    nameToid:"",//负责人页面显示名字,提交时要提交id
    nameToidid:"",
    pic_no:"",
    p_no:"",
    fszjth:"",//附属组件图号
    //----------------------下方是工序单 单个工序
    
    showsel:false,
    process:[],//工序名称列表 id: 1, name: "TEST01", desc: "TEST01", content: "TEST01", prepare: "1.5h"
    process_name:[],//只储存↑name
    selNum : 0,

    //-----------------------------↓是新的 上方准备都删除
    tmpFInterID:'',//FInterID
    tmpFEntryID:'',//FEntryID
    tmpFItemID:'',//FItemID
    processes:[],//工序列表
    tmpdelIdList:[],//这里面，用来存放，你删除掉的工序id,如果id为0直接删除就可以无需写入此数组
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    that.setData({
      tmpFInterID:options.FInterID,
      tmpFEntryID:options.FEntryID,
      tmpFItemID:options.FItemID,
    })
    this.getList()

  },

  getList(){
    var that = this
    let params = {
      FInterID:this.data.tmpFInterID,
      FEntryID:this.data.tmpFEntryID,
      FItemID:this.data.tmpFItemID,
    }
    request.requestGet('order/get_process', params, '',function (res) {
      that.setData({
        processes:res.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  bindKeyInputVant1(e){
      var that = this
      var index = e.currentTarget.dataset.index;
      var variable = e.currentTarget.dataset.variable;
      var arr = this.data.processes;
      arr[index][variable] = e.detail
      this.setData({
        processes:that.data.processes
      })
  },

  onShow() {
    
  },
  delinfo(e){
    let currentList = this.data.processes
      //NOTE 获取传来的index
      var index = e.currentTarget.dataset.index;
      let isInsert = false
      //将删除的id插入到this.data.delIdList,注意如果已经有此id或者id为0不传入
      if(this.data.processes[index].id !== 0){
        for(let i = 0;i<this.data.tmpdelIdList.length;i++ ){
          if(this.data.tmpdelIdList[i] === this.data.processes[index].id){
            isInsert = true
          }
        }
      }
      let tmp = this.data.tmpdelIdList
      if(isInsert === false){
        tmp.push(this.data.processes[index].id)
        currentList.splice(index, 1)
        this.setData({
          processes: currentList
        })
      }else{
        wx.showToast({
          title: '已经删除此工序',
          icon:"error"
        })
      }
      this.setData({
        tmpdelIdList:tmp
      })
  },

  upinfo(e){
    if(e.currentTarget.dataset.index === 0){
      return
    }else{
      let tmpNum = e.currentTarget.dataset.index - 1;
      let processes = this.data.processes; // 获取数组引用
      // 直接交换数组元素
      [processes[e.currentTarget.dataset.index], processes[tmpNum]] = 
      [processes[tmpNum], processes[e.currentTarget.dataset.index]];
      // 整体设置数组
      this.setData({
        processes: processes
      });
    }
  },
  downinfo(e){
    if(e.currentTarget.dataset.index === this.data.processes.length-1){
      return
    }else{
      let tmpNum = e.currentTarget.dataset.index + 1;
      let processes = this.data.processes; // 获取数组引用
      // 直接交换数组元素
      [processes[e.currentTarget.dataset.index], processes[tmpNum]] = 
      [processes[tmpNum], processes[e.currentTarget.dataset.index]];
      // 整体设置数组
      this.setData({
        processes: processes
      });
    }
  },
  submit(){
    var that = this
    let tmp = {
      FInterID: that.data.tmpFInterID,
      FEntryID: that.data.tmpFEntryID,
      FItemID: that.data.tmpFItemID,
      "processes": that.data.processes,
      "delIdList": that.data.tmpdelIdList
    }
    request.requestAjax('order/save_process', tmp, '提交中',function (res) {
      if(res.status === 200){
        wx.showToast({
          title: res.msg,
          icon:"success"
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon:"error"
        })
      }
    })
  },
  addInfo(e){
    let currentList = this.data.processes
    var that =this
    let tmp = that.data.processes.length + 1
    currentList.push({FEntryID:Number(that.data.tmpFEntryID) ,FInterID:Number(that.data.tmpFInterID),FItemID:Number(that.data.tmpFItemID),content:'',desc:'', id:0,name:'',quota:'',review_time:'', sort:tmp,tracking_commission:''})
    this.setData({
      processes:currentList
    })
  },


  setSel(e){
    let index = e.currentTarget.dataset.index
    var that = this
    if(e.currentTarget.dataset.name === 'material_req'){
      that.data.processes[index].material_req = !that.data.processes[index].material_req
      if(that.data.processes[index].material_req === true){
        // console.log('应该挑选领取物料页面')
        wx.navigateTo({ url: '/pages/material_list/material_list' })
      }
    }else if(e.currentTarget.dataset.name === 'weighing'){
      that.data.processes[index].weighing = !that.data.processes[index].weighing
    }else if(e.currentTarget.dataset.name === 'warehousing'){
      that.data.processes[index].warehousing = !that.data.processes[index].warehousing
    }else if(e.currentTarget.dataset.name === 'outsourcing'){
      that.data.processes[index].outsourcing = !that.data.processes[index].outsourcing
    }else if(e.currentTarget.dataset.name === 'print_label'){
      that.data.processes[index].print_label = !that.data.processes[index].print_label
    }
    this.setData({
      processes:that.data.processes
    })
  }
})




